// +----------------------------------------------------------------------
// | Manong.Cloud [ 领酷码农云 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.manong.cloud All rights reserved.
// +----------------------------------------------------------------------
// | Author: 稻草人 <qinuoyun@qq.com>
// +----------------------------------------------------------------------
// | Member: 围棋 飘逸者 Loumt Silence24
// +----------------------------------------------------------------------
import DataUtil from './../utils/DataUtil'

class ContactController extends BasicController {

    /**
     * 砸到机用户验证
     * @return {[type]} [description]
     */
    async actionIndex() {
        let data = JSON.parse(DataUtil.RequestRSADecrypt($_POST['data']));
        let return_data = {
            status: false,
            userType: '',
            IdNo: '',
            effect: '',
            expire: '',
            FaceOption: ''
        };
        let effect = '';
        let expire = '';
        if (data.code) {
            let door = await this.modules('Building.LockDoor').where({ code: data.code }).first();
            if (!door) {
                throw new Error('机器码错误');
            }
            let building_id = door.building_id;
            let time = parseInt(Date.now() / 1000);
            let user = null;
            if (data.content) {
                let dec = decipher256(data.content);
                let params = getParams(dec);
                if (params.time) {
                    if (parseInt(params.time) <= time) {
                        throw new Error('二维码失效');
                    }
                }
                if (params.uid) {
                    user = await this.modules('User').where({ id: params.uid }).first();
                }
            }
            if (data.id_card) {
                user = await this.modules('User').where({ id_card: data.id_card }).first();
            }
            if (!user) {
                throw new Error('用户不存在');
            }
            let UID = user.id;
            let id_card = user.id_card;

            let visitor_data = null;
            if (user.company_id) {
                let company = await this.modules('Company').where({ id: user.company_id }).first();
                if (company.building_id == building_id) {
                    visitor_data = {
                        UID,
                        id_card,
                        building_id,
                        company_id: user.company_id,
                        type: 1
                    }
                    effect = new Date(new Date().toLocaleDateString()).getTime() / 1000
                    expire = (new Date(new Date().toLocaleDateString()).getTime() / 1000) + 86400
                }
            }

            if (!visitor_data) {
                let invite = await this.modules('Visitor.Invite').where({ UID, building_id, status: 1 }).order([
                    ['id', 'DESC']
                ]).first();
                let time_str = date('Y-m-d H:i:s', time);
                if (invite && invite.start_time <= time_str && invite.end_time >= time_str) {
                    visitor_data = {
                        UID,
                        id_card,
                        building_id,
                        company_id: invite.company_id,
                        type: 2
                    }
                    effect = new Date(invite.start_time).getTime() / 1000
                    expire = new Date(invite.end_time).getTime() / 1000
                }
            }
            if (visitor_data) {
                let focus = await this.modules('Visitor.Focus').where({ id_card }).first();
                visitor_data['status'] = focus ? 2 : 1;
                this.modules('Visitor').insert(visitor_data);

                return_data = {
                    status: true,
                    userType: visitor_data.type,
                    IdNo: id_card,
                    effect,
                    expire,
                    FaceOption: user.face_option ? user.face_option : 0
                };
            }
        } else {
            throw new Error('机器码缺失');
        }
        return DataUtil.ResponseRSAEncrypt(JSON.stringify(return_data));
    };

}

export default ContactController;